package com.lile.generic;

/*
 * 泛型在默认情况下，是Object类型。
 */
public class ToolTest {

	public static void main(String[] args) {
		// test1();
		// test2();
		test3();
	}

	/*
	 * 用Object可以让我们的程序提高扩展性。 但是问题是，隐含了类型转换异常。 所以，这种解决方案不是特别的好。
	 * 这个时候，java就提供了泛型技术。
	 */
	public static void test1() {
		Student s = new Student();
		Teacher t = new Teacher();

		// Tool t = new Tool();
		// t.setStudent(s);
		// Student ss = t.getStudent();
		// ss.love();

		Tool tool = new Tool();
		// 设置为Student
		tool.setObj(s);
		Student ss = (Student) tool.getObj();
		ss.love();

		// 设置为Teacher
		tool.setObj(t);
		Teacher tt = (Teacher) tool.getObj();
		tt.love();

		Student sss = (Student) tool.getObj();
		sss.love();
	}

	public static void test2() {
		Student s = new Student();
		Teacher t = new Teacher();

		// Tool2 tool = new Tool2();
		// // 设置为Student
		// tool.setQq(s);
		// Student ss = (Student) tool.getQq();
		// ss.love();
		// // 设置为Teacher
		// tool.setQq(t);
		// Teacher tt = (Teacher) tool.getQq();
		// tt.love();
		// Student sss = (Student) tool.getQq();
		// sss.love();

		Tool2<Student> tool = new Tool2<Student>();
		tool.setQq(s);
		Student ss = tool.getQq();
		ss.love();

		// tool.setQq(t); // 编译期间就告诉你，你错了，我已经明确了，Student类型

		Tool2<Teacher> tool2 = new Tool2<Teacher>();
		tool2.setQq(t);
		Teacher tt = tool2.getQq();
		tt.love();
	}

	public static void test3() {
		Tool3 tool3 = new Tool3();

		tool3.print("hello");
		tool3.print(100);
		tool3.print(true);
	}

}
